(function(){
    // Ext.data.Model类似java中的class
    function testDefineExtDataModel(){
        // 定义Model
        Ext.define("Person", {
            extend: "Ext.data.Model",
            // fields字段数组存放Ext.data.Field
            fields: [
                {name: "name", type: "auto"},
                {name: "age", type: "auto"},
                {name: "email", type: "auto"}
            ]
        });
        // 等同于Ext.regModel，省略extend
        // 4.0.7 已放弃
        Ext.regModel("User", {
            fields: [
                {name: "name", type: "auto"},
                {name: "age", type: "auto"},
                {name: "email", type: "auto"}
            ]
        });
        
        // 创建Model实例
        var p = new Person({
            name: "P0"
        });
        // 得用get(filedName)获得字段值
        alert(p.get("name"));
        // 推荐使用这种方法
        var p1 = Ext.create("Person", {
            name: "P1"
        });
        alert(p1.get("name"));
        // ModelManager创建
        var p2 = Ext.ModelManager.create({
            name: "P2"
        }, "Person");
        alert(p2.get("name"));
         
        // 其他Model方法
        // 获得Model名
        alert(Person.getName());
    }
    
    // 数据验证 Ext.data.validations
    function testExtDataValidations(){
        Ext.define("Person", {
            extend: "Ext.data.Model",
            // fields字段数组存放Ext.data.Field
            fields: [
                {name: "name", type: "string"},
                {name: "age", type: "int"},
                {name: "email", type: "auto"}
            ],
            // 存放Ext.data.validations
            // field字别写错了
            validations: [
                {type: "length", field: "name", min: 2, max: 6}
            ]
        });
        var p = Ext.create("Person", {
            name: "ppppppp",
            age: 1,
            email: "aa@163.com"
        });
        
        // 修改错误信息提示
        Ext.data.validations.lengthMessage = "错误的长度";
        
        // 进行验证，错误返回 Ext.data.Errors
        var errors = p.validate();
        var errorInfos = [];
        errors.each(function(item, index, length){
            console.dir(arguments);
            errorInfos.push(item.field + " " + item.message);
        });
        alert(errorInfos.join("\n"));
        
        var nameErrors = errors.getByField("name");
        for(var i=0, len=nameErrors.length; i<len; i++){
            var item = nameErrors[i];
            errorInfos.push(item.field + " " + item.message);
        }
        alert(errorInfos.join("\n"));
    }
    
    // 自定义数据验证
    // 方法一：js方法对Ext.data.validations扩展
    // 方法二：使用Ext.apply对Ext的对象进行改造
    function testDefineDataValidations(){
        // apply方法的参数分别是原生对象、扩展属性、默认值；applyif没有的属性才添加，已有的属性不改变
        Ext.apply(Ext.data.validations, {
            // 参考validations的length方法的源码
            age: function(config, value){
                // 配置项中的属性
                var min = config.min;
                var max = config.max;
                if(min <= value && value <= max){
                    return true;
                }else{
                    this.ageMessage += "，应该在[" + min + "~" + max + "]";
                    return false;
                }
            },
            // message命名：对应的验证类型名称 + Message
            ageMessage: 'age数据不符合规定'
        });
        
        Ext.define("Person", {
            extend: "Ext.data.Model",
            // fields字段数组存放Ext.data.Field
            fields: [
                {name: "name", type: "string"},
                {name: "age", type: "int"},
                {name: "email", type: "auto"}
            ],
            // 存放Ext.data.validations
            // field字别写错了
            validations: [
                {type: "age", field: "age", min: 0, max: 200}
            ]
        });
        
        var p = Ext.create("Person", {
            name: "kk",
            age: 201
        });
        
        var errors = p.validate();
        errors.each(function(item, index, length){
            alert(item.field + " " + item.message);
        });
    } 
    
    // Ext.data.proxy.Proxy：数据代理，用来完成CRUD操作的对象 
    function testExtDataProxy(){
        Ext.define("Person", {
            extend: "Ext.data.Model",
            // fields字段数组存放Ext.data.Field
            fields: [
                {name: "name", type: "string"},
                {name: "age", type: "int"},
                {name: "email", type: "auto"}
            ],
            // 配置了代理，在实例化时需要使用load方法来加载实例，不同于Ext.create时直接填充数据
            proxy: {
                type: "ajax",
                url: webroot + "/lesson03/ajaxdataproxy"
            }
        });
        
        // 通过代理来获得实例
        var p = Ext.ModelManager.getModel("Person");
        // id=1
        p.load(1, {
            scope: this,
            failure: function(record, operation) {
            },
            success: function(record, operation) {
                // TODO: record就是Person的实例吗？
                alert(record.data.name);
                alert(record.get("name"));
            },
            callback: function(record, operation) {
            }
        });
        // alert(p.get("name")); // 不能这样
    }
    
    // store
    
    // Ext.data.Model的many-to-one和one-to-many
    function testExtDataModelOne2Many(){
        Ext.define("Teacher", {
            extend: "Ext.data.Model",
            fields: [
                {name: "id", type: "int"},
                {name: "name", type: "string"},
                {name: "age", type: "int"},
                {name: "email", type: "auto"}
            ],
            // Ext.data.HasManyAssociation 配置一对多
            hasMany: {
                model: 'Student',
                // t.students()获得store数据集合
                name: 'students'
            }
        });
        Ext.define("Student", {
            extend: "Ext.data.Model",
            fields: [
                {name: "id", type: "int"},
                {name: "name", type: "string"},
                {name: "age", type: "int"},
                {name: "email", type: "auto"}
            ]
        });
    }
    
    var webroot;
    
    Ext.onReady(function(){
        webroot = Ext.get("webroot").getValue();
//        testDefineExtDataModel();
//        testExtDataValidations();
//        testDefineDataValidations();
//        testExtDataProxy();
//        testExtDataModelOne2Many();
    });
})();